import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomePageState();
  }
}

const APPBAR_SCROLL_OFFSET=100;
class _HomePageState extends State<HomePage> {
  double appBarAlpha=0;
  // 图片地址
  List<String> _imageUlrs = [
    "https://dimg04.c-ctrip.com/images/a10915000000xdmfo60F6.jpg",
    "https://dimg04.c-ctrip.com/images/a10i15000000xptxoA856.jpg",
    "https://dimg02.c-ctrip.com/images/a10s0u000000jbef9A566.jpg",
  ];

  _onScroll(offset) {
    print(offset);
    double alpha = offset / APPBAR_SCROLL_OFFSET;

    // 判断滚动范围
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });

    print("首页appbar 滚动透明度:$appBarAlpha");
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: <Widget>[
        MediaQuery.removePadding(context:context,removeTop: true, child: NotificationListener(
          onNotification:(ScrollNotification) {
            if(ScrollNotification is ScrollUpdateNotification && ScrollNotification.depth==0){
              _onScroll(ScrollNotification.metrics.pixels);
            }
          },
          child: ListView(
            children: <Widget>[
              Container(
                  height: 160,
                  child: Swiper(
                    itemCount: _imageUlrs.length,
                    autoplay: true,
                    itemBuilder: (BuildContext context, int index) {
                      return Image.network(_imageUlrs[index], fit: BoxFit.fill,);
                    },
                    pagination: SwiperPagination(),
                  )),
              Container(
                height: 800,
                child: ListTile(
                  title: Text('哈哈'),
                ),
              )
            ],
          ),
        )
        ),
        Opacity(
          opacity: appBarAlpha,
          child: Container(
          height: 80,
          decoration: BoxDecoration(color:Colors.white),
          child: Center(
            child: Padding(
              padding: EdgeInsets.only(top: 20),
              child: Text('首页'),
            ),
          ),
        ),)
      ],),
    );
  }
}
